#include "uid.h"
#include "uid_map.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

const int elements = 20;

int main(int c, char** v)
{
    uidmap_t map = emptyUIDMap();

    uid_t* names  = malloc(sizeof(uid_t)*elements);
    
    int i;
    int j;
    for(i = 0; i < elements; i++)
    {
	//names[i]  = uidGenerateSingle();
	bool taken = true;
	while(taken)
	{
	    names[i] = (rand() % 998) + 1;

	    taken = false;
	    for(j = 0; j<i; j++)
	    {
		taken = taken || (names[i] == names[j]);
	    }
	}

	map = setUIDMap(names[i], names[i] + 1, map);

	assert(validateStructure(map));
	validateMap(map);

	for(j = 0; j < i; j++)
	    assert(names[j] + 1 == getUIDMap(names[j], 0, map));
    }

    int old_val;
    
    for(i = 0; i < elements; i++)
    {
	map = delUIDMap(names[i], &old_val, map);
	//assert(old_val == (names[i] + 1));

	assert(validateStructure(map));
	validateMap(map);

	dumpMap(map, 0);
	
	//printf("%ld : %d\n", names[i], getUIDMap(names[i], 0, map));
	assert(0 == getUIDMap(names[i], 0, map));

	for(j = i+1; j < elements; j++)
	{
	    printf("Wanted %ld, got %d\n", names[j] + 1, getUIDMap(names[j], 0, map));
	    assert(names[j] + 1 == getUIDMap(names[j], 0, map));
	}
    }
    
    freeUIDMap(map);

    return 0;
}
